<template>
    <div class="html-format-container">
        <h1>Html格式化工具</h1>
        <p>这是一个可以将原始HTML代码格式化为带有高亮效果的HTML代码的工具。</p>
        <div class="content-wrapper">
            <div class="output-column">
                <div class="radio-group">
                    <label>
                        <input type="radio" v-model="displayMode" value="format" checked /> Html格式化
                    </label>
                    <label>
                        <input type="radio" v-model="displayMode" value="preview" /> Html预览
                    </label>
                </div>
                <!-- <md-editor v-model="formattedHtml" :previewOnly="true" :theme="theme" style="height: 30vw"
                    language="en-US"/> -->
                <!-- <MdPreview :modelValue="content" previewTheme="github" codeTheme="github" /> -->
                <div class="main-content">
                    <div class="panel">
                        <div class="input-section">
                            <label for="html-input">原始Html数据:</label>
                            <textarea id="html-input" v-model="inputHtml" placeholder="请输入Html数据..."
                                @input="clearOutput"></textarea>
                        </div>
                        <div class="controls" aria-hidden="false">
                            <button @click="htmlFormat" title="Base64 编码" aria-label="编码 (左到右)">
                                转化 →
                            </button>
                        </div>
                        <div class="output-section">
                            <label>格式化结果:</label>
                            <MdPreview v-if="formattedHtml" :modelValue="formattedHtml" :codeFoldable="false"
                                class="md-preview-container" />
                            <div class="output-placeholder" v-else>
                                格式化后的Html将在此处显示
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
const inputHtml = ref<string>('')
const formattedHtml = ref<string>('')
import { html as beautifyHtml } from 'js-beautify'
import { MdPreview } from 'md-editor-v3'
// import {MdEditor} from 'md-editor-v3'

// const text = ref('# Hello md-editor-v3 👋\n\n- 支持实时预览\n- 支持图片上传\n- 支持快捷键（如 Ctrl+S 保存）')
import 'md-editor-v3/lib/style.css'


// const formattedHtml = ref('')
const displayMode = ref<'format' | 'preview'>('format')
// const theme = ref<'light' | 'dark'>('light')
// function handleSave(v: string, html: string) {
//   console.log('保存内容：', v)
//   console.log('渲染 HTML：', html)
//   localStorage.setItem('post.md', v)
// }

// 清空输出内容
function clearOutput() {
    formattedHtml.value = ''
}

function htmlFormat() {
    try {
        if (displayMode.value === 'format') {
            const formatted = beautifyHtml(inputHtml.value, {
                indent_size: 2,
                wrap_line_length: 80,
                unformatted: []
            })
            formattedHtml.value = '```html\n' + formatted + '\n```'
        } else if (displayMode.value === 'preview') {
            formattedHtml.value = inputHtml.value
        }

    } catch (e: unknown) {
        // 忽略错误
        console.error('HTML格式化出错:', e);
    }
}

</script>

<style scoped>
.md-preview-container {
    height: 30vw;
    width: 100%;
    flex-direction: row;
    /* 水平滚动 */
    overflow-x: auto;
    /* 垂直滚动 */
    overflow-y: auto;
}

.main-content {
    min-height: 60%;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.panel {
    display: flex;
    flex: 1;
    gap: 20px;
    overflow: hidden;
}

.input-section,
.output-section {
    max-width: 45vw;
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 8px;
    overflow: hidden;
}

.input-section label,
.output-section label {
    font-weight: bold;
    font-size: 14px;
}

.output-content {
    height: 100%;
    max-height: 30vw;
    padding: 12px;
    overflow-x: auto;
    /* 水平滚动 */
    overflow-y: auto;

    word-break: keep-all;
    word-wrap: normal;
    width: 100%;
    max-width: 50vw;
    box-sizing: border-box;
}

.output-placeholder {
    height: 100%;
    /* 最大高度 */
    padding: 12px;
    border: 1px solid #d0d7de;
    border-radius: 6px;
    background-color: #f6f8fa;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #6c757d;
    font-style: italic;
}

.controls {
    width: var(--controls-width);
    display: flex;
    flex-direction: column;
    gap: 8px;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
    margin: 0px 10px 0px 10px;
}

.html-format-container {
    padding: 20px;
}

.content-wrapper {
    display: flex;
    margin-top: 20px;
    gap: 20px;
}

.input-column,
.output-column {
    flex: 1;
    display: flex;
    flex-direction: column;
}

.button-column {
    display: flex;
    align-items: center;
    justify-content: center;
}

textarea {
    width: 100%;
    height: 30vw;
    padding: 10px;
    border: 1px solid #ccc;
    border-radius: 4px;
    font-family: 'Courier New', monospace;
    resize: vertical;
    background-color: #f8f8f8;
}

.radio-group {
    margin-bottom: 10px;
}

.radio-group label {
    margin-right: 15px;
    font-weight: bold;
}

.radio-group input[type="radio"] {
    margin-right: 5px;
}

button {
    padding: 10px 20px;
    background-color: #007bff;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 16px;
}

button:hover {
    background-color: #0056b3;
}
</style>